package ge.telasi.tasks;

import ge.telasi.tasks.controller.UserController;
import ge.telasi.tasks.model.Structure;
import ge.telasi.tasks.model.User;
import java.io.IOException;
import javax.persistence.EntityManager;

/**
 * Prepares database with sample data.
 *
 * @author dimitri
 */
public class PrepareDatabaseWithSampleData {

    public static void main(String[] args) throws IOException {
        try {
            EntityManager em = PersistenceUtils.createEmFactory().createEntityManager();
            User user = em.find(User.class, 3);

//            PersistenceUtils.beginTransaction(em);
//            user.setPassword("dima123");
//            PersistenceUtils.endTransaction(em);
//            System.out.println(user.getPassword());
            
//            createAdmin(em);
//            String schemaName = PersistenceUtils.getSchema();
//            new Migration().cleanAndBuildDatabase(em, schemaName);
//            new StructureFixture().getEntities(em);
//            new UserFixture().getEntities(em);
//            new FlowPermissionFixture().getEntities(em);
//            new TaskFixture().getEntities(em);
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }

    static void createAdmin(EntityManager em) throws Exception {
        User user = new User();
        user.setStructure(em.find(Structure.class, 4));
        user.setUsername("dimitri");
        user.setFirstName("დიმიტრი");
        user.setLastName("ყურაშვილი");
        user.setFirstName2("Дмитрий");
        user.setLastName2("Курашвили");
        user.setCode("3015");
        user.setPosition("პროგრამისტი");
        user.setLocation("ცენტრ. ფილიალი, მე-3 სართული");
        user.setMobile("895335514");
        user.setWorkPhone("868");
        user.setEmail("dimakura@gmail.com");
        PersistenceUtils.beginTransaction(em);
        user = new UserController().register(em, user, "dima123");
//        if (user.isNew()) {
//            user = new UserCopy().createClone(user);
//            user.setNew(false);
//            new UserController().updateUserByAdmin(em, user, user);
//        }
        PersistenceUtils.endTransaction(em);
    }

}
